package com.cqcetbbs.smartlab.cqcetbbs.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import com.cqcetbbs.smartlab.cqcetbbs.R;
import com.cqcetbbs.smartlab.cqcetbbs.adapter.ViewPagerAdapter;
import com.cqcetbbs.smartlab.cqcetbbs.fragment.FragmentChat;
import com.cqcetbbs.smartlab.cqcetbbs.fragment.FragmentDetailed;
import com.cqcetbbs.smartlab.cqcetbbs.fragment.FragmentMain;
import com.cqcetbbs.smartlab.cqcetbbs.fragment.FragmentMy;
import com.cqcetbbs.smartlab.cqcetbbs.view.BottomNavigationViewHelper;
import com.cqcetbbs.smartlab.cqcetbbs.view.NoScrollViewPager;

import net.youmi.android.nm.sp.SpotManager;

import okhttp3.OkHttpClient;

/**
 * 主页面
 */
public class MainActivity extends AppCompatActivity {

    public static final String MIANACTIVITY = MainActivity.class.getSimpleName();

    private ViewPagerAdapter adapter;
    private NoScrollViewPager vp_page;
    private BottomNavigationView bottomNavigationView;
    private long mExitTime;
    OkHttpClient client = new OkHttpClient();
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {


        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    //初始化界面
    private void initView() {
        vp_page = (NoScrollViewPager) findViewById(R.id.vp_page);
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigation);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
    }


    //初始化数据
    private void initData() {

        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        // 为Adapter添加Fragment
        adapter.addFragment(new FragmentMain());
        adapter.addFragment(new FragmentDetailed());
        adapter.addFragment(new FragmentChat());
        adapter.addFragment(new FragmentMy());
        vp_page.setAdapter(adapter);

        // 为 BottomNavigationView 的菜单项  设置监听事件
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                // 获取到菜单项的Id
                int itemId = item.getItemId();
                // 当第一项被选择时BlankFragmentOne显示，以此类推
                switch (itemId) {
                    case R.id.bottom_nav_ui:
                        vp_page.setCurrentItem(0);
                        break;
                    case R.id.bottom_nav_data:
                        vp_page.setCurrentItem(1);
                        break;
                    case R.id.bottom_nav_service:
                        vp_page.setCurrentItem(2);
                        break;
                    case R.id.bottom_nav_net:
                        vp_page.setCurrentItem(3);
                        break;
                }
                // true 会显示这个Item被选中的效果 false 则不会
                return true;
            }
        });
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Object mHelperUtils;
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
                SpotManager.getInstance(this).onAppExit();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
